home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / ixemul / sdk / man / cat3 / stdarg.0 < prev    next >
Encoding:
Text File  |  1998-06-15  |  4.8 KB  |  107 lines

  1.  
  2. STDARG(3)                  UNIX Programmer's Manual                  STDARG(3)
  3.  
  4. NNAAMMEE
  5.      ssttddaarrgg - variable argument lists
  6.  
  7. SSYYNNOOPPSSIISS
  8.      ##iinncclluuddee <<ssttddaarrgg..hh>>
  9.  
  10.      _v_o_i_d
  11.      vvaa__ssttaarrtt(_v_a___l_i_s_t _a_p, _l_a_s_t)
  12.  
  13.      _t_y_p_e
  14.      vvaa__aarrgg(_v_a___l_i_s_t _a_p, _t_y_p_e)
  15.  
  16.      _v_o_i_d
  17.      vvaa__eenndd(_v_a___l_i_s_t _a_p)
  18.  
  19. DDEESSCCRRIIPPTTIIOONN
  20.      A function may be called with a varying number of arguments of varying
  21.      types.  The include file <_s_t_d_a_r_g_._h> declares a type (_v_a___l_i_s_t) and defines
  22.      three macros for stepping through a list of arguments whose number and
  23.      types are not known to the called function.
  24.  
  25.      The called function must declare an object of type _v_a___l_i_s_t which is used
  26.      by the macros vvaa__ssttaarrtt(), vvaa__aarrgg(), and vvaa__eenndd().
  27.  
  28.      The vvaa__ssttaarrtt() macro initializes _a_p for subsequent use by vvaa__aarrgg() and
  29.      vvaa__eenndd(), and must be called first.
  30.  
  31.      The parameter _l_a_s_t is the name of the last parameter before the variable
  32.      argument list, i.e. the last parameter of which the calling function
  33.      knows the type.
  34.  
  35.      Because the address of this parameter is used in the vvaa__ssttaarrtt() macro, it
  36.      should not be declared as a register variable, or as a function or an ar-
  37.      ray type.
  38.  
  39.      The vvaa__ssttaarrtt() macro returns no value.
  40.  
  41.      The vvaa__aarrgg() macro expands to an expression that has the type and value
  42.      of the next argument in the call.  The parameter _a_p is the _v_a___l_i_s_t _a_p
  43.      initialized by vvaa__ssttaarrtt().  Each call to vvaa__aarrgg() modifies _a_p so that the
  44.      next call returns the next argument.  The parameter _t_y_p_e is a type name
  45.      specified so that the type of a pointer to an object that has the speci-
  46.      fied type can be obtained simply by adding a * to _t_y_p_e.
  47.  
  48.      If there is no next argument, or if _t_y_p_e is not compatible with the type
  49.      of the actual next argument (as promoted according to the default argu-
  50.      ment promotions), random errors will occur.
  51.  
  52.      The first use of the vvaa__aarrgg() macro after that of the vvaa__ssttaarrtt() macro
  53.      returns the argument after _l_a_s_t. Successive invocations return the values
  54.      of the remaining arguments.
  55.  
  56.      The vvaa__eenndd() macro handles a normal return from the function whose vari-
  57.      able argument list was initialized by vvaa__ssttaarrtt().
  58.  
  59.      The vvaa__eenndd() macro returns no value.
  60.  
  61. EEXXAAMMPPLLEESS
  62.      The function _f_o_o takes a string of format characters and prints out the
  63.      argument associated with each format character based on the type.
  64.  
  65.            void foo(char *fmt, ...)
  66.            {
  67.                    va_list ap;
  68.                    int d;
  69.                    char c, *p, *s;
  70.  
  71.                    va_start(ap, fmt);
  72.                    while (*fmt)
  73.                            switch(*fmt++) {
  74.                            case 's':                       /* string */
  75.                                    s = va_arg(ap, char *);
  76.                                    printf("string %s\n", s);
  77.                                    break;
  78.                            case 'd':                       /* int */
  79.                                    d = va_arg(ap, int);
  80.                                    printf("int %d\n", d);
  81.                                    break;
  82.                            case 'c':                       /* char */
  83.                                    c = va_arg(ap, char);
  84.                                    printf("char %c\n", c);
  85.                                    break;
  86.                            }
  87.                    va_end(ap);
  88.            }
  89.  
  90. SSTTAANNDDAARRDDSS
  91.      The vvaa__ssttaarrtt(), vvaa__aarrgg(), and vvaa__eenndd() macros conform to ANSI C3.159-1989
  92.      (``ANSI C'').
  93.  
  94. CCOOMMPPAATTIIBBIILLIITTYY
  95.      These macros are _n_o_t compatible with the historic macros they replace.  A
  96.      backward compatible version can be found in the include file <_v_a_r_a_r_g_s_._h>.
  97.  
  98. BBUUGGSS
  99.      Unlike the _v_a_r_a_r_g_s macros, the ssttddaarrgg macros do not permit programmers to
  100.      code a function with no fixed arguments.  This problem generates work
  101.      mainly when converting _v_a_r_a_r_g_s code to ssttddaarrgg code, but it also creates
  102.      difficulties for variadic functions that wish to pass all of their argu-
  103.      ments on to a function that takes a _v_a___l_i_s_t argument, such as
  104.      vfprintf(3).
  105.  
  106. BSD Experimental                 June 5, 1993                                2
  107.